<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
//for stock

define('COLOR_NAME', 'Farve');
define('SIZE_NAME', 'Størrelse');

ini_set('max_execution_time', 900);
set_time_limit(900);
echo '<pre>';

function group_assoc($array, $key) {
    $return = array();
    foreach($array as $v) {
        $return[$v[$key]][] = $v;
    }
    return $return;
}

function toArray($obj)
{
    if (is_object($obj)) $obj = (array)$obj;
    if (is_array($obj)) {
        $new = array();
        foreach ($obj as $key => $val) {
            $new[$key] = toArray($val);
        }
    } else {
        $new = $obj;
    }
    return $new;
}

function addColortoproduct($db, $product_id, $quantity, $attribute_id, $arr_colors){

	/*
	$db->delete('product_id = ' . $product_id, 'product_option');
	$db->delete('product_id = ' . $product_id, 'product_option_description');
	$db->delete('product_id = ' . $product_id, 'product_option_value');
	$db->delete('product_id = ' . $product_id, 'product_option_value_description');
	*/	
	$product_option_row = $db->fetch_row('product_id = ' . $product_id . ", attribute_id = " . $attribute_id, 'product_option', FETCH_ASSOC);
	$exists = $db->get_rows();
	if(empty($exists)){
		$fields = array('product_id','attribute_id');
		$data = array($product_id, $attribute_id);
		$product_option_id = $db->insert($fields, $data, 'product_option');
		
		$fields = array('product_option_id','language_id','product_id','name');
		$data = array($product_option_id, '1', $product_id, COLOR_NAME );
		$db->insert($fields, $data, 'product_option_description');
	}else{
		$product_option_id = $product_option_row[0]['product_option_id'];
	}	
	foreach($arr_colors as $color){
	
		$attribute_value_id = getAttributeValueId($db, $attribute_id, $color );		
		$product_option_value_row = $db->fetch_row('product_option_id = ' . $product_option_id . 
		", product_id = " . $product_id .
		", attribute_id = " .$attribute_id . ", attribute_value_id = " . $attribute_value_id, 'product_option_value', FETCH_ASSOC);
		$exists1 = $db->get_rows();
		if(empty($exists1)){
			$fields = array('product_option_id','product_id','quantity','attribute_id','attribute_value_id');
			$data = array($product_option_id, $product_id, $quantity , $attribute_id, $attribute_value_id);
			$product_option_value_id = $db->insert($fields, $data, 'product_option_value');
			
			$fields = array('product_option_value_id','language_id','product_id','name');
			$data = array($product_option_value_id, '1', $product_id, $color);
			$db->insert($fields, $data, 'product_option_value_description');
		}
	}
	
}

function getStockcount($ean, $arrStock){
	$count = '0';
	foreach($arrStock as $key=>$value){
		if($ean == $value['Key']){
			$count = $value['Value'];
			break;
		}
	}
	return $count;
}

function getAttributeId($db, $KatalogNumber, $name){
	$attribute = $db->fetch_row('katalognumber = ' . $KatalogNumber .", name = '" . $name ."'" , 'attribute' , FETCH_ASSOC);
	$exists = $db->get_rows();
	if(!empty($exists)){
		return $attribute[0]['attribute_id'];
	}
	return '';
}

function getAttributeValueId($db, $attribute_id, $name){
	
	$attribute_value = $db->fetch_row('attribute_id = ' . $attribute_id .", name = '" . $name . "'", 'attribute_value', FETCH_ASSOC);
	
	$exists = $db->get_rows();
	
	
	if(!empty($exists)){
		return $attribute_value[0]['value_id'];
	}
	
	return '';

}

require_once('simplesql.class.php5');

$ini = parse_ini_file("web.ini");
while(list($key,$value) = each($ini)){
	if($key == 'hostName'){
		$hostName = $value;
	}
	else if($key == 'userName'){
		$userName = $value;
	}
	else if($key == 'password'){
		$password = $value;
	}
	else if($key == 'dbName'){
		$dbName = $value;
	}
}

$db = new simplesql($hostName,$userName,$password,$dbName);
$db->query("SET NAMES utf8");
//$db->showlog = true;
	$myarticles = file_get_contents('cache.txt');
	$objInfo = unserialize($myarticles);
	$arrInfo = toArray($objInfo);
	
	$mystock = file_get_contents('stock.txt');
	$objStock = unserialize($mystock);
	$arrStock = toArray($objStock);
	
	$action = isset($_GET['action'])?$_GET['action']:"insert";
	
	$group_products = group_assoc($arrInfo, 'KatalogNumber');
	
	$error = false;
	$db->query("BEGIN");
	foreach($group_products as $KatalogNumber=>$products){
		$my_product = $db->fetch_query('SELECT `product_id` FROM product WHERE katalognumber = ' . $KatalogNumber, FETCH_ASSOC);
		if(count($my_product)){
			$product_id = $my_product[0]['product_id'];
			$ean = $products[0]['Ean'];
			//$quantity = getStockcount($ean , $arrStock);
			$quantity = '0';
			
			$group_product_color = group_assoc($products, 'Vparam1');
			//var_dump($group_product_color);
			//get array with color name
			$arr_colors = array_keys($group_product_color);
			$color_attribute_id = getAttributeId($db, $KatalogNumber, COLOR_NAME);
			addColortoproduct($db, $product_id, $quantity, $color_attribute_id, $arr_colors);		
		}
	}
	
	if(!$error)  
	{  
	//*** Commit Transaction ***//  
		$db->query("COMMIT");  
		echo "OK <br />";  
		
	}  
	else  
	{  
		//*** RollBack Transaction ***//  
		echo "Error:  [".$db->get_lastquery()."]" ."-" . mysql_error();  
		$db->query("ROLLBACK");  
	}
	
	echo '</pre>';


?>
</body>
</html>